﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using UscSunShine.Models;
using System.Data.Entity;
using PagedList;


namespace UscSunShine.Mvc.Controllers
{
    [AllowAnonymous]
    public class HomeController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        public ActionResult Index()
        {
            MainPageModel mainPageModel = new MainPageModel();

            var status = UscSunShine.Models.Request.StatusCanBrowse;
            
            //获取统计信息
            //按处理状态分组，统计申请数量
            var result = db.Requests.Where(t => status.Contains(t.ProcessStatus))
                            .GroupBy(t => t.ProcessStatus)
                            .Select(s => new { Key = s.Key, Count = s.Count() })
                            .ToList();

            //统计信息
            mainPageModel.StatisticModel = new Statistics
            {
                CountOfRequest = result.Sum(t => t.Count),
                CountOfProcessing = result.Where(t => UscSunShine.Models.Request.StatusProcessing.Contains(t.Key))
                                          .Sum(t => t.Count),
                CountOfProcessed = result.Where(t => UscSunShine.Models.Request.StatusProcessed.Contains(t.Key))
                                         .Sum(t => t.Count)
            };
            //公告信息
            mainPageModel.PostList = db.Posts.OrderByDescending(t => t.InputDate)
                                             .Take(15).ToList();
            //阳光服务信息台
            mainPageModel.RequestList = db.Requests.Include(t => t.Department)
                                                   .Where(t => status.Contains(t.ProcessStatus))
                                                   .OrderByDescending(t => t.AcceptDate)
                                                   .Take(30)
                                                   .ToList();
            //办事机构
            mainPageModel.DepartmentList = db.Departments.Take(8).ToList();


            return View(mainPageModel);
        }

        public ActionResult WorkFlow(int? currentDepartmentID, int? page)
        {
            var department = db.Departments.OrderBy(m => m.ID);
            ViewBag.departmentList = department;
            ViewBag.currentDepartment = currentDepartmentID;

            var posts = from s in db.Posts
                        where s.PostCategory == PostCategory.办事流程
                        select s;
            if (currentDepartmentID != null)
            {
                posts = posts.Where(s => s.DepartmentID == currentDepartmentID);
            }
            posts = posts.OrderByDescending(s => s.InputDate);
            ViewBag.postCount = posts.Count();

            int pageSize = 14;
            int pageNum = (page ?? 1);
            return View(posts.ToPagedList(pageNum, pageSize));
        }

        public ActionResult Policy(int? currentDepartmentID, int? page)
        {
            var department = db.Departments.OrderBy(m => m.ID);
            ViewBag.departmentList = department;
            ViewBag.currentDepartment = currentDepartmentID;
            var posts = from s in db.Posts
                        where s.PostCategory == PostCategory.政策法规
                        select s;
            if (currentDepartmentID != null)
            {
                posts = posts.Where(s => s.DepartmentID == currentDepartmentID);
            }
            posts = posts.OrderByDescending(s => s.InputDate);
            ViewBag.postCount = posts.Count();

            int pageSize = 14;
            int pageNum = (page ?? 1);
            return View(posts.ToPagedList(pageNum, pageSize));
        }

        public ActionResult Contactme()
        {
            return View();
        }

        public ActionResult Speaking()
        {
            return View();
        }

        public ActionResult LeaderMail()
        {
            return View();
        }

    }
}